package com.clss.commandhelper.function.homefragment4th;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.clss.commandhelper.R;
import com.clss.commandhelper.base.BaseFragment;
import com.clss.commandhelper.function.casedetail.CaseDetailActivity;
import com.clss.commandhelper.tools.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuv
 * @date 2017/12/5
 * 应急视频
 */

public class Home4thFragment extends BaseFragment implements Home4thPresenter.Home4thView,
        AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{
    private static final String TAG = "Home2ndFragment";

    private Home4thPresenter mHome4thPresenter;
    private SwipeRefreshLayout mHome4thSrl;
    private ListView mHome4thLv;
    private TaskAdapter mTaskAdapter;
    private List<LiveVideoTaskEntity> mLiveVideoList = new ArrayList<>();

    @Override
    protected int setViewId() {
        return R.layout.fragment_home4th;
    }

    @Override
    protected void initView(View v) {
        mHome4thSrl = v.findViewById(R.id.home_4th_srl);
        mHome4thLv = v.findViewById(R.id.home_4th_lv);
        mHome4thSrl.setOnRefreshListener(this);

        mHome4thPresenter = new Home4thPresenter();
        mHome4thPresenter.setHome4thView(this);

        mTaskAdapter = new TaskAdapter(getContext(), 0);
        mHome4thLv.setAdapter(mTaskAdapter);
        mHome4thLv.setOnItemClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        getList();
    }

    private void getList(){
        if (!mHome4thSrl.isRefreshing()){
            mHome4thSrl.setRefreshing(true);
        }
        mHome4thPresenter.getTaskList();
    }

    private void stopRefreshing(){
        mHome4thSrl.setRefreshing(false);
    }

    @Override
    public void requestFailure(Throwable t) {
        stopRefreshing();
    }

    @Override
    public void responseError(int code, String msg) {
        stopRefreshing();
        showShortToast(msg);
    }


    @Override
    public void getTaskListSuccess(List<LiveVideoTaskEntity> liveVideoTaskEntityList) {
        mLiveVideoList.clear();
        mLiveVideoList.addAll(liveVideoTaskEntityList);

        mTaskAdapter.notifyDataSetChanged();
        stopRefreshing();
    }

    @Override
    public void onRefresh() {
        getList();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Bundle bundle = new Bundle();
        bundle.putString(CaseDetailActivity.TASK_ID, mLiveVideoList.get(position).getPoliceInfoTaskId());
        bundle.putString(CaseDetailActivity.ID, mLiveVideoList.get(position).getId());
        bundle.putInt(CaseDetailActivity.DISPATCH_STATUS, mLiveVideoList.get(position).getDispatchStatus());
        bundle.putString(CaseDetailActivity.AREA_CODE, mLiveVideoList.get(position).getAreaCode());
        Utils.startActivity(getActivity(), CaseDetailActivity.class, bundle);
    }
    
    class TaskAdapter extends ArrayAdapter {
        private TaskAdapter(@NonNull Context context, int resource) {
            super(context, resource);
        }

        @Override
        public int getCount() {
            return mLiveVideoList.size();
        }

        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            LiveVideoTaskEntity liveVideoTaskEntity = mLiveVideoList.get(position);
            View v = getLayoutInflater().inflate(R.layout.item_task_list, parent, false);
            TextView timeTv = v.findViewById(R.id.task_item_time);
            TextView addressTv = v.findViewById(R.id.task_item_address);
            TextView nameTv = v.findViewById(R.id.task_item_name);
            TextView statusTv = v.findViewById(R.id.task_item_state);
            if (liveVideoTaskEntity.getDispatchStatus() == 1){
                statusTv.setText("未认领");
            }else if (liveVideoTaskEntity.getDispatchStatus() == 2){
                statusTv.setText("正在处理");
            }else if (liveVideoTaskEntity.getDispatchStatus() == 3){
                statusTv.setText("已完成");
            }
            timeTv.setText(liveVideoTaskEntity.getCreateTime());
            addressTv.setText(liveVideoTaskEntity.getPoliceInfoAddress());
            if (liveVideoTaskEntity.getTaskName() != null){
                nameTv.setText(liveVideoTaskEntity.getTaskName());
            }
            return v;
        }
    }
}
